package com.uzai.console.service.groupsendjob.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.uzai.common.enums.PlatformTypeEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.dto.groupsendjob.GroupSendJobConditionOrderDto;
import com.uzai.console.entity.*;
import com.uzai.console.mapper.*;
import com.uzai.console.service.groupsendjob.GroupSendJobConditionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author mingyang.chen create in 2023/5/22 17:16
 */
@Component
@Slf4j
public class GroupSendJobConditionServiceImpl implements GroupSendJobConditionService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private JdOrderMapper jdOrderMapper;
    @Autowired
    private PddOrderMapper pddOrderMapper;
    @Autowired
    private VphOrderMapper vphOrderMapper;
    @Autowired
    private MtOrderMapper mtOrderMapper;
    @Autowired
    private ElmOrderMapper elmOrderMapper;
    @Autowired
    private DyOrderMapper dyOrderMapper;
    @Autowired
    private KsOrderMapper ksOrderMapper;

    @Override
    public List<String> checkOrderConditionWithUserIds(GroupSendJobConditionOrderDto conditionOrder) {

        if (Objects.isNull(conditionOrder) || Objects.isNull(conditionOrder.getMerId()) || ObjectUtils.isEmpty(conditionOrder.getBizUserIdList()) || Objects.isNull(conditionOrder.getPlatform())) {
            throw new BusinessException("检查订单条件参数错误");
        }
        log.info("checkOrderConditionWithUserIds param: {}", JSON.toJSONString(conditionOrder));
        PlatformTypeEnum platform = PlatformTypeEnum.getById(conditionOrder.getPlatform());
        if (Objects.isNull(platform)) {
            throw new BusinessException("平台不存在");
        }
        List<String> result;
        switch (platform) {
            case PT_TAOBAO:
                List<TbOrder> tbOrders = tbOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(tbOrders, TbOrder::getBizUserId, TbOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_JD:
                List<JdOrder> jdOrders = jdOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(jdOrders, JdOrder::getBizUserId, JdOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_PDD:
                List<PddOrder> pddOrders = pddOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(pddOrders, PddOrder::getBizUserId, PddOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_VPH:
                List<VphOrder> vphOrders = vphOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(vphOrders, VphOrder::getBizUserId, VphOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_MT_LM:
                List<MtOrder> mtOrders = mtOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(mtOrders, MtOrder::getBizUserId, MtOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_ELM:
                List<ElmOrder> elmOrders = elmOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(elmOrders, ElmOrder::getBizUserId, ElmOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_DY:
                List<DyOrder> dyOrders = dyOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(dyOrders, DyOrder::getBizUserId, DyOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            case PT_KS:
                List<KsOrder> ksOrders = ksOrderMapper.selectExistsOrderUserIdWithCondition(conditionOrder);
                result = doCommonOptAndFilterTitle(ksOrders, KsOrder::getBizUserId, KsOrder::getTitle, conditionOrder.getOrderTitleList());
                break;
            default:
                return Lists.newArrayList();
        }
        log.info("checkOrderConditionWithUserIds result: {}", JSON.toJSONString(result));
        return result;
    }

    public <T> List<String> doCommonOptAndFilterTitle(List<T> orderList, Function<T, String> field, Function<T, String> titleField, List<String> filterStr) {
        if (ObjectUtils.isEmpty(orderList)) {
            return Lists.newArrayList();
        }
        List<String> orderTitleList = filterStr;
        if (ObjectUtils.isEmpty(orderTitleList)) {
            return orderList.stream().map(field).distinct().collect(Collectors.toList());
        }
        String[] orderTitleArray = orderTitleList.toArray(new String[orderTitleList.size()]);
        // 返回
        return orderList.stream().filter(item -> StringUtils.containsAny(titleField.apply(item), orderTitleArray)).map(field).distinct().collect(Collectors.toList());
    }
}
